package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;
import org.omg.PortableInterceptor.INACTIVE;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    public Object listCart(HttpServletRequest request){
        Cart cart = new Cart();
        if(request.getParameter("businessId")!=null) {
            cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        }
        if(request.getParameter("userId")!=null) {
            cart.setUserId(request.getParameter("userId"));
        }
        CartService service = new CartServiceImpl();
        List<Cart> list = service.listCart(cart);
        return list;
    }
    public Object saveCart(HttpServletRequest request){
        CartService cartService = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
//        cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
        int result=cartService.saveCart(cart);
        return result;
    }
    public Object updateCart (HttpServletRequest request){
        CartService cartService = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        int result=cartService.updateCart(cart);
        return result;
    }
    public  Object removeCart(HttpServletRequest request){
        Cart cart = new Cart();
//        if (Integer.valueOf(request.getParameter("foodId"))!=null){
            cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
//        }
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        CartService service = new CartServiceImpl();
        int result = service.removeCart(cart);
        return result;
    }

}
